/*
$Id:$

ST7565 LCD library!

Copyright (C) 2010 Limor Fried, Adafruit Industries

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

 // some of this code was written by <cstone@pobox.com> originally; it is in the public domain.
*/

#include <delay.h>
#include <spi.h>
//#include <stdlib.h>
#include "stlcd.h"
#include "glcd.h"
#include "logo_glcd.h"
#include "../main.h"
//#include "icon.h"


unsigned char is_reversed = 0;

int pagemap[] = { 7, 6, 5, 4, 3, 2, 1, 0};



extern StructChrono CurrentLap;
extern unsigned char uc_CurrentLap;
extern unsigned long ul_TimeCounter_0_5sec;
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

void clear_screen(void) {
  unsigned char p, c;
  
  for(p = 0; p < 8; p++) {
    /*
      putstring("new page! ");
      uart_putw_dec(p);
      putstring_nl("");
    */

    st7565_command(CMD_SET_PAGE | pagemap[p]);
    st7565_command(CMD_SET_COLUMN_LOWER | 3);
    st7565_command(CMD_SET_COLUMN_UPPER | 0);
    st7565_command(CMD_RMW);
    st7565_data(0xff);
    
    for(c = 0; c < 128; c++) {
      //uart_putw_dec(c);
      //uart_putchar(' ');

      st7565_data(0x00);
    }     
  }
}


void ST7565_Init(void) {
    int i;
    //LCD Pins init
    
    TRISBbits.TRISB9 = 0;   //SO1
    TRISCbits.TRISC6 = 0;   //CLK1
    TRISCbits.TRISC7 = 0;   //AO1
    TRISCbits.TRISC8 = 0;   //RST1
    TRISCbits.TRISC9 = 0;   //CS1
    /*
    TRISBbits.TRISB9 = 0;   //CS1
    TRISCbits.TRISC6 = 0;   //RST1
    TRISCbits.TRISC7 = 0;   //AO1
    TRISCbits.TRISC8 = 0;   //CLK1
    TRISCbits.TRISC9 = 0;   //SO1
    */
    // toggle RST low to reset;

    CS_PIN = 1;
    for(i=0;i<100;i++);
    RST_PIN = 0;
    for(i=0;i<100;i++);
    RST_PIN = 1;
    //SPI Init
    OpenSPI1(ENABLE_SCK_PIN&ENABLE_SDO_PIN&SPI_MODE16_OFF&SLAVE_ENABLE_OFF&SPI_CKE_OFF&CLK_POL_ACTIVE_LOW&MASTER_ENABLE_ON&SEC_PRESCAL_1_1&PRI_PRESCAL_1_1,
             FRAME_ENABLE_OFF&FRAME_SYNC_OUTPUT&FRAME_POL_ACTIVE_LOW,
             SPI_ENABLE&SPI_IDLE_CON&SPI_RX_OVFLOW_CLR);
    for(i=0;i<100;i++);
    CS_PIN = 0;
    for(i=0;i<100;i++);
    // LCD bias select
    st7565_command(CMD_SET_BIAS_7);
    Nop();
    // ADC select
    st7565_command(CMD_SET_ADC_NORMAL);
    Nop();
    // SHL select
    st7565_command(CMD_SET_COM_REVERSE);
    Nop();
    // Initial display line
    st7565_command(CMD_SET_DISP_START_LINE);
    Nop();
    // turn on voltage converter (VC=1, VR=0, VF=0)
    st7565_command(CMD_SET_POWER_CONTROL | 0x4);
    // wait for 50% rising

    // turn on voltage regulator (VC=1, VR=1, VF=0)
    st7565_command(CMD_SET_POWER_CONTROL | 0x6);
    Nop();


    // turn on voltage follower (VC=1, VR=1, VF=1)
    st7565_command(CMD_SET_POWER_CONTROL | 0x7);
    Nop();

    // set lcd operating voltage (regulator resistor, ref voltage resistor)
    st7565_command(CMD_SET_RESISTOR_RATIO | 0x6);
    Nop();
    st7565_command(CMD_DISPLAY_ON);
    Nop();
  // initial display line
  // set page address
  // set column address
  // write display data
}


void st7565_command(unsigned char c) {

  //CS_PIN = 0;
  A0_PIN = 0;
  WriteSPI1(c);
  while(SPI1STATbits.SPITBF == 1);
  //CS_PIN = 1;
}

void st7565_data(unsigned char c) {
  //CS_PIN = 0;
  A0_PIN = 1;
  WriteSPI1(c);
  while(SPI1STATbits.SPITBF == 1);
  //CS_PIN = 1;
}
void st7565_set_brightness(unsigned char val) {
    st7565_command(CMD_SET_VOLUME_FIRST);
    st7565_command(CMD_SET_VOLUME_SECOND | (val & 0x3f));
}


void write_buffer(unsigned char *buffer) {
  unsigned char c, p;

  for(p = 0; p < 8; p++) {
    /*
      putstring("new page! ");
      uart_putw_dec(p);
      putstring_nl("");
    */

    st7565_command(CMD_SET_PAGE | pagemap[p]);
    st7565_command(CMD_SET_COLUMN_LOWER | 3);
    st7565_command(CMD_SET_COLUMN_UPPER | 0);
    st7565_command(CMD_RMW);
    st7565_data(0xff);
    
    //st7565_data(0x80);
    //continue;
    
    for(c = 0; c < 128; c++) {
      //uart_putw_dec(c);
      //uart_putchar(' ');
      //st7565_data(buffer[(128*p)+(127-c)]);
      st7565_data(buffer[(128*p)+(127-c)]);
    }
  }
}

